<?php
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$plugin_name =$this->pluginname;
if($_RQ['op']=='list'){
	if($_RQ['id']){
		$listid=intval($_RQ['id']);
		$this->session('listid',$listid);
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 15;
	$params=array('pluginname'=>$plugin_name,'merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid']);
	if($listid){
		$params['listid']=$listid;
		$return['params']['listid']=$listid;
	}
	//批号
	if(!empty($_RQ['code'])){
		$params['code']=intval($_RQ['code']);
		$return['params']['code']=$_RQ['code'];
	}
	//备注
	if(!empty($_RQ['remark'])){
		$params['remark']='%'.trim($_RQ['remark']).'%';
		$return['params']['remark']=$_RQ['remark'];
	}
    $fields='';
    $orderby='id desc';
	$total='';
	$list=pdo_getslice('jy_kabao_batch',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	foreach($list as &$item){
		$item['listtitle']=pdo_getcolumn('jy_kabao_list',array('id'=>$item['listid']),'title');
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$return['status']=array('1'=>'正常','0'=>'失效');		
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'批次管理'),
			array('op'=>'add','title'=>'添加批次'),
		),
		'list'=>array(
		'tip'=>'卡包的<em>业务逻辑</em>：</br>
				1、每个卡包，生成一批次，卡包码；生成卡包码时可以绑定大夫</br>
				2、找印刷商，每行生成一个二维码，批量印刷二维码（<em>温馨提示</em>：下载的只是字符串/链接地址。印刷商，会帮你转成二维码），如果直接下载二维码，文件太大，没什么意义。；</br>
				3、每个二维码印刷一个卡片',
		'data'=>'list','edit'=>false,'list'=>array(
			array('name'=>'listtitle','title'=>'所属卡包','type'=>'string'),
			array('name'=>'num','title'=>'卡包码数量','type'=>'string'),
			array('name'=>'type','title'=>'类型','type'=>'radio','options'=>'batchtypes'),
			array('type'=>'select','name'=>'storeid','title'=>'绑定医馆','options'=>'stores','show'=>array('type'=>array(1,2))),
			array('type'=>'select','name'=>'workerid','title'=>'绑定大夫','options'=>'workers','show'=>array('type'=>2),'option_title'=>'name'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'time'),
			array('name'=>'remark','title'=>'备注','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
		),
		'action'=>array(
			array('type'=>'post','op'=>'down_code','title'=>'下载'),
			array('type'=>'href','op'=>'codelist','title'=>'查看卡包码'),
		)
		),
		'search'=>array(			
			array('name'=>'listid','width'=>15,'title'=>'所属卡包ID','type'=>'choose','op'=>'choose_goods'),
			array('name'=>'code','width'=>15,'title'=>'批号','type'=>'string'),
			array('name'=>'remark','width'=>15,'title'=>'备注','type'=>'string'),
		)
	);
	$return['htmls']=$htmls;
	$return['batchtypes']=$this->batchtypes;
	$return['kabaos']=pdo_getall('jy_kabao_list',array('pluginname'=>$plugin_name,'uniacid'=>$_SESSION['uniacid']),array('title','id'),'id');
	$return['stores']=pdo_getall('jy_yiliao_store',array('uniacid'=>$_SESSION['uniacid']),array('title','id'),'id');
	$return['workers']=pdo_getall('jy_yiliao_worker',array('uniacid'=>$_SESSION['uniacid']),array('name','id'),'id');
	exi($return);
}
if($_RQ['op']=='add'){
	if($_RQ['postdata']){
		$postdata=get_postdata();
		$num=intval($postdata['num']);
		if(!$num){
			exi('请先设置生成数量！','error');
		}
		$listid=intval($postdata['listid']);
		if(!$listid){
			exi('请先选择卡包！','error');
		}
		$return=model('jy_kabao/batch')->add_batch($postdata);
		if($return){
			exi('卡包码生成成功！请前往列表页下载！','','jy_yiliao/merch.kabao.batch/list');
		}else{
			exi('添加失败','error','jy_yiliao/merch.kabao.batch/list');
		}
	
	}	
	$item=array('pluginname'=>$plugin_name,'merchid'=>$merchid,'type'=>0,'listid'=>$this->session('listid'));
    $return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'',
		'desc'=>'模板设置',
		'tabs'=>array(
			array('op'=>'list','title'=>'批次管理'),
			array('op'=>'edit','title'=>'添加批次')
		),
		'edit'=>array(
			array('type'=>'select','name'=>'listid','title'=>'所属卡包','options'=>'kabaos'),
			array('name'=>'num','title'=>'生成卡码数量','type'=>'unitstring','unit'=>'个'),
			array('name'=>'type','title'=>'归属类型','type'=>'radio','options'=>'batchtypes'),
			array('type'=>'select','name'=>'storeid','title'=>'绑定医馆','options'=>'stores','show'=>array('type'=>array(1,2))),
			array('type'=>'select','name'=>'workerid','title'=>'绑定大夫','options'=>'workers','show'=>array('type'=>2),'option_title'=>'name'),
			array('name'=>'remark','title'=>'备注','type'=>'string'),
		)
	);
	$return['htmls']=$htmls;
	$return['batchtypes']=$this->batchtypes;
	$return['kabaos']=pdo_getall('jy_kabao_list',array('pluginname'=>$plugin_name,'merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid']),array('title','id'),'id');
	$return['stores']=pdo_getall('jy_yiliao_store',array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid),array('title','id'),'id');
	$return['workers']=pdo_getall('jy_yiliao_worker',array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid),array('name','id'),'id');	
	exi($return);
}
if($_RQ['op']=='codelist'){
	if($_RQ['id']){
		$batchid=intval($_RQ['id']);
		$this->session('batchid',$batchid);
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 15;
	$params=array('pluginname'=>$plugin_name,'merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid']);
	if($batchid){
		$params['batchid']=$batchid;
		$return['params']['batchid']=$batchid;
	}
	if(!empty($_RQ['type'])){
		$params['type']=trim($_RQ['type']);
		$return['params']['type']=$_RQ['type'];
	}
	if(!empty($_RQ['listid'])){
		$params['listid']=trim($_RQ['listid']);
		$return['params']['listid']=$_RQ['listid'];
	}
	if(!empty($_RQ['storeid'])){
		$params['storeid']=trim($_RQ['storeid']);
		$return['params']['storeid']=$_RQ['storeid'];
	}
	if(!empty($_RQ['workerid'])){
		$params['workerid']=trim($_RQ['workerid']);
		$return['params']['workerid']=$_RQ['workerid'];
	}
	if(!empty($_RQ['status'])){
		$params['status']=trim($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	if(!empty($_RQ['sn'])){
		$params['sn']='%'.trim($_RQ['sn']).'%';
		$return['params']['sn']=$_RQ['sn'];
	}
    $fields='';
    $orderby='id desc';
	$total='';
	$list=pdo_getslice('jy_kabao_batch_code',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	foreach($list as &$item){
		$item['member']=cfc('core')->member_show($item['mid']);
		$item['listtitle']=pdo_getcolumn('jy_kabao_list',array('id'=>$item['listid']),'title');
		$item['url']=custom_url('jy_kabao/item//code='.$item['sn'],true);
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$return['codestatus']=$this->codestatus;		
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'codelist','title'=>'卡码列表'),
		),
		'list'=>array('data'=>'list','edit'=>false,'del_op'=>'codedel','list'=>array(
			array('name'=>'listtitle','title'=>'所属卡包','type'=>'string'),
			array('name'=>'sn','title'=>'编号','type'=>'string'),
			array('name'=>'type','title'=>'类型','type'=>'radio','options'=>'batchtypes'),
			array('type'=>'select','name'=>'storeid','title'=>'绑定医馆','options'=>'stores'),
			array('type'=>'select','name'=>'workerid','title'=>'绑定大夫','options'=>'workers','option_title'=>'name'),
			array('name'=>'url','title'=>'二维码','type'=>'ewm'),
			array('name'=>'url','title'=>'链接','type'=>'copy'),
			array('name'=>'member','title'=>'领卡人','type'=>'member'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'codestatus'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'time'),
		),
		),
		'search'=>array(
			array('name'=>'type','width'=>10,'title'=>'类型','type'=>'radio','options'=>'batchtypes'),
			array('type'=>'select','width'=>15,'name'=>'listid','title'=>'所属卡包','options'=>'kabaos'),
			array('type'=>'select','width'=>15,'name'=>'storeid','title'=>'绑定医馆','options'=>'stores'),
			array('type'=>'select','width'=>10,'name'=>'workerid','title'=>'绑定大夫','options'=>'workers','option_title'=>'name'),
			array('name'=>'sn','width'=>10,'title'=>'编号','type'=>'string'),
			array('name'=>'status','width'=>10,'title'=>'状态','type'=>'radio','options'=>'codestatus'),
		)
	);
	$return['batchtypes']=$this->batchtypes;
	$return['kabaos']=pdo_getall('jy_kabao_list',array('pluginname'=>$plugin_name,'merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid']),array('title','id'),'id');
	$return['stores']=pdo_getall('jy_yiliao_store',array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid),array('title','id'),'id');
	$return['workers']=pdo_getall('jy_yiliao_worker',array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid),array('name','id'),'id');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='down_code'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择批次','error','list');	
	}
	$file=pdo_getcolumn('jy_kabao_batch',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),'code_file');
	$file=tomedia($file);
	exi('','','',$file);
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_kabao_batch',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_kabao_batch',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			}
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='codedel'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_kabao_batch_code',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_kabao_batch_code',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			}
		}
	}
	exi('删除成功！');
}
?>